package org.wutopia.labcloud.domain.product.service.impl.dubbo;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.wutopia.labcloud.domain.product.dao.ProductMainDao;
import org.wutopia.labcloud.domain.product.dao.ProductSpecificationDao;
import org.wutopia.labcloud.domain.product.dao.ProductStockpileDao;
import org.wutopia.labcloud.domain.product.dao.entity.mysql.ProductMain;
import org.wutopia.labcloud.domain.product.dao.entity.mysql.ProductSpecification;
import org.wutopia.labcloud.domain.product.dao.entity.mysql.ProductStockpile;
import org.wutopia.labcloud.service.api.service.dubbo.DubboProductService;
import org.wutopia.labcloud.service.api.service.dubbo.dto.output.DubboGetProductOutputDTO;
import org.wutopia.labcloud.service.api.service.dubbo.dto.output.DubboProductSpecificationOutput;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@DubboService
@Slf4j
public class DubboProductServiceImpl implements DubboProductService {

    private ProductMainDao productMainDao;

    private ProductSpecificationDao productSpecificationDao;

    private ProductStockpileDao productStockpileDao;


    @Override
    public DubboGetProductOutputDTO getProductById(Long id) {

        ProductMain productMain = productMainDao.selectById(id);


        QueryWrapper<ProductStockpile> q = new QueryWrapper<>();
        q.eq("product_id", id);
        ProductStockpile productStockpile = productStockpileDao.selectOne(q);


        QueryWrapper<ProductSpecification> q2 = new QueryWrapper<>();
        q2.eq("product_id", id);
        List<ProductSpecification> productSpecifications = productSpecificationDao.selectList(q2);

        List<DubboProductSpecificationOutput> specList = new ArrayList<>();

        for(ProductSpecification spec : productSpecifications) {
            DubboProductSpecificationOutput specOut = new DubboProductSpecificationOutput();
            specOut.setId(spec.getId());
            specOut.setItem(spec.getItem());
            specOut.setValue(spec.getValue());
            specList.add(specOut);
        }

        DubboGetProductOutputDTO vo = new DubboGetProductOutputDTO();
        vo.setId(productMain.getId());
        vo.setCreateTime(productMain.getCreateTime());
        vo.setUpdateTime(productMain.getUpdateTime());
        vo.setTitle(productMain.getTitle());
        vo.setDescription(productMain.getDescription());
        vo.setPrice(productMain.getPrice());
        vo.setRate(productMain.getRate());
        vo.setCoverAddr(productMain.getCoverAddr());
        vo.setDetailAddr(productMain.getDetailAddr());

        vo.setStockpile(productStockpile.getAmount());

        vo.setSpecificationList(specList);

        return vo;
    }


}
